layui.use(['form', 'table'], function(){
	var table = layui.table;
	var form = layui.form;
	
	initTable(table);
	
	initForm(form);
	
});

//初始化表格
function initTable(table){
	//表格数据定义
	table.render({
		elem: '#demo'
		,height: 312
		,url: '/base/article/pageList'
		,height: 'full-100'
		,page: true
		,limit: 10
		,limits: [5,10,20,50]
		,cols: [[
			{title: '序号', width:100, sort: true, fixed: 'left', align: 'center', templet: '#xuhao'}
			,{title: '操作', sort: true, align: 'center', templet: '#btnTemplate'}
			,{field: 'ARTICLENAME', title: '文章名称', sort: true, align: 'center'}
			,{title: '操作日期', sort: true, align: 'center', templet: '#dateTemplate'}
		]]
	});
	
	//表格事件定义
	table.on('tool(test)', function(obj){
		var data = obj.data; //获得当前行数据
		var layEvent = obj.event; //获得 lay-event 对应的值
		if(layEvent === 'del'){ //删除
			layui.layer.confirm('确定删除？', function(index){
				obj.del();
				del(data.ID);
				layer.close(index);
			});
		}
		if(layEvent === 'setType'){
			setType(data.ID);
		}
		if(layEvent === 'setLabel'){
			setLabel(data.ID);
		}
	});
}

//初始化表单
function initForm(form){
	initSelect(form);
}

//初始化下拉选
function initSelect(form){
	$.ajax({
		type: 'post',
		url: '/base/article/getAllType',
		dataType: 'json',
		success: function(result){
			var code = result.code;
			var html = '';
			if(code==0){
				var data = result.data;
				for(var i=0;i<data.length;i++){
					var map = data[i];
					var id = map.id;
					var type = map.articleType;
					html += '<option value="' + id + '">' + type + '</option>';
				}
				$("#articleType").append(html);
				form.render();
			}
		},
		error: function(e){
			layui.layer.msg('网络异常', {icon: 5});
		}
	});
	
	form.on('select(articleType)', function(data){
		var type = data.value;
		selectType(type);
	}); 
}

//新增或修改
function add(id){
	var ids = "";
	if(id!=undefined && id!=null){
		ids = "?id=" + id;
	}
	var layer = layui.layer;
	var index = layer.open({
		type: 2
		,title: '文章编写'
		,content: '/base/article/addPage' + ids
		,closeBtn: 1
		,maxmin: true
	});
	layer.full(index);
}

//删除
function del(id){
	$.ajax({
		type: 'get',
		url: '/base/article/del?id=' + id,
		dataType: 'json',
		success: function(result){
			var code = result.code;
			if(code==0){
				layui.layer.msg('删除成功', {icon: 6},function(){
					refresh();//刷新表格
				});	
			}else{
				layui.layer.msg('删除失败', {icon: 5});
			}
		},
		error: function(e){
			layui.layer.msg('网络异常', {icon: 5});
		}
	});
}

//刷新
function refresh(){
	layui.table.reload('demo');
}

//设置分类
function setType(id){
	layui.layer.open({
		type: 2,
		title: false,
		content: '/base/article/addType?id=' + id,
		area: ['500px', '250px'],
		shade: 0.5
	});
}

//设置标签
function setLabel(id){
	layui.layer.open({
		type: 2,
		title: false,
		content: '/base/article/addLabel?id=' + id,
		area: ['600px', '400px'],
		shade: 0.5
	});
}

//文章类型下拉选
function selectType(type){
	reload({'type': type});
}

//刷新表格数据
function reload(data){
	layui.table.reload('demo', {
		where: data
	});
}